<?php
class PublicController extends Zendvn_Controller_Action{
	
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	protected $_namespace;
	
	public function init(){
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] 
									 . '/' . $this->_arrParam['controller'];
		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/login';	

							 
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//$ssFilter->unsetAll();
		if(empty($ssFilter->col)){
			$ssFilter->keywords 	= '';
		}
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		
		
		/*====================================================
		 * Load Zendvn Translate for Controller
		 *====================================================*/
		/*$this->_langObj->setLangFile(array('default.language.tmx'));		
		Zend_Registry::set('Zend_Translate', $this->_langObj->generate());*/
		
		$info = new Zendvn_System_Info();
		
		$language = $info->getLanguage('admin');
		if(empty($language)){
			$language = 'vi';
		}
		$translate = array(
							'adapter' => 'tmx',
							'content' => LANG_PATH . '/' . $language . '/admin/default.language.tmx',
							'locale' => $language,
							);
		$translate = new Zend_Translate($translate);
		Zend_Registry::set('Zend_Translate', $translate);
		
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
		
		$template_path = TEMPLATE_PATH . "/login/system";
		$this->loadTemplate($template_path,'template.ini','template');
	}
	
	public function errorAction(){
		$this->view->Title = 'Message: Error!';
		$this->view->headTitle($this->view->Title,true);
		$error[] = 'Chuc nang nay khong ton tai.';
		$this->view->messageError = $error;
	}
	public function noAccessAction(){
		$this->view->Title = 'No Access';
		$this->view->headTitle($this->view->Title,true);
		$error[] = 'Ban khong quyen truy cap vao chuc nang nay.';
		$this->view->messageError = $error;
		$this->_helper->viewRenderer('error');
		$this->_redirect('/default/index/index/');
	}
	
	public function loginAction(){
		$this->view->Title 		= 'Đăng nhập';
		$this->view->headTitle($this->view->Title,true);
		$info = new Zendvn_System_Info();
		$memberInfo = $info->getMemberInfo('id');
		if($memberInfo != ''){
			$this->_redirect('/default/index/index/');
		}	
		
		$template_path = TEMPLATE_PATH . "/public/system";
		$this->loadTemplate($template_path,'template.ini','template');
		$this->view->Title = 'Login';
		$this->view->headTitle($this->view->Title,true);		
		if($this->_request->isPost()){
			$yourCart = new Zend_Session_Namespace('default-index');			
			unset($yourCart->shoppingcart);
			$validator = new Default_Form_ValidateLogin($this->_arrParam);
			if($validator->isError() == true){
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
			}else{
				$auth = new Zendvn_System_Auth();
				if($auth->login($this->_arrParam)){				
					$info = new Zendvn_System_Info();
					$info->createInfo();
					$this->_redirect('/default/admin/index/');
				}else{
					$this->view->result = $auth->getError();
				}
			}
		}
		
	}	
	public function ajaxLoginOrderAction(){
		$this->_helper->layout->disableLayout();
		$yourCart = new Zend_Session_Namespace('default-index');
		// validate login
		if(isset($this->_arrParam["user_name"]) && isset($this->_arrParam['password'])){
			$validator = new Default_Form_ValidateLogin($this->_arrParam);
			if($validator->isError() == true){
				
				$this->view->loginMessageError = $validator->getMessageError();
				$this->view->loginItem	= $validator->getData();
			}else{
				
				$auth = new Zendvn_System_Auth();
				if($auth->login($this->_arrParam)){				
					$info = new Zendvn_System_Info();
					$info->createInfo();
					//$this->_redirect('/default/admin/index/');
				}else{
					$this->view->loginResultMsg = $auth->getError();
				}
			}
		}
		
		$info = new Zendvn_System_Info();
		$memberInfo = $info->getMemberInfo('id');
		if($memberInfo != ''){
			$tbltUser 			= new Zendvn_Models_Users();
			$this->view->User 	= $tbltUser->listItem($this->arrParam,array('user_id'=>$memberInfo,'task'=>'public'));
		}		
		// validate order
		$this->_arrParam['cart'] = (array) $yourCart->shoppingcart;
		if(isset($this->_arrParam["name"]) && isset($this->_arrParam['phone']) &&  isset($this->_arrParam['address'])){
			$validator = new Products_Form_ValidateInvoice($this->_arrParam);
			if($validator->isError() == true){
				$this->view->errors = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
			}else{
				
				if(count($this->_arrParam['cart']) > 0){
					$tblInvoice 					= new Zendvn_Models_Invoice();
					$invoice_id 					= $tblInvoice->saveItem($this->_arrParam,array('task'=>'public-order'));			
					$tblInvoiceDetail 				= new Zendvn_Models_InvoiceDetail();
					$this->_arrParam['invoice_id'] 	= $invoice_id;
					$tblInvoiceDetail->saveItem($this->_arrParam);
					unset($yourCart->shoppingcart);
					
					$this->view->successMessage = true;
				}else{
					$this->view->successMessage = false;
				}
			}
		}
		
		$info = new Zendvn_System_Info();
		$memberInfo = $info->getMemberInfo('id');
		if($memberInfo != ''){
			$tbltUser 			= new Zendvn_Models_Users();
			$this->view->User 	= $tbltUser->listItem($this->arrParam,array('user_id'=>$memberInfo,'task'=>'public'));
		}	
	}
	public function logoutAction(){
		$this->view->Title = 'Logout';
		$this->view->headTitle($this->view->Title,true);
		$auth = new Zendvn_System_Auth();
		$auth->logout();
		
		$info = new Zendvn_System_Info();
		$info->destroyInfo();
		
		$link = $this->view->baseUrl('/default/index/index');
		$this->view->Notes = 'Ban da thoat he thong.
							<a style="color:#dafa03;text-decoration: underline" href="' . $link . '">Nhan vao day</a> de qua lai trang chu
						';
		
		//$this->_helper->viewRenderer('error');
	}
	
	public function registerAction(){
		$template_path = TEMPLATE_PATH . "/public/system";
		$this->loadTemplate($template_path,'template.ini','template');
		$this->view->Title = 'Đăng ký thành viên';
		$this->view->headTitle($this->view->Title,true);
		$captcha = new Zendvn_Captcha_Image();
		$this->view->captcha = $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();
		if($this->_request->isPost()){
			$captchaID = $this->_arrParam['captchaID'];
			$validator = new Default_Form_ValidateRegister($this->_arrParam);
			if($validator->isError() 	  == true){				
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item 		  = $validator->getData();
			}else{
				$arrParam = $validator->getData(array('upload'=>false));
				$tblUser 	= new Zendvn_Models_Users();
				$tblUser->saveItem($arrParam,array('task'=>'user-register'));
				$this->_redirect('/default/public/notice');
			}
			$captcha->removeImg($captchaID);
		}
		$captcha->removeImg($this->view->captcha_id,array('exception'=>true));
	}
	public function resetPassAction(){
		if(isset($this->_arrParam['code'])){
			$tblUser = new Zendvn_Models_Users();
			$new_pass = Zendvn_Encode::RandomString();
			$params = $tblUser ->getItem($this->_arrParam, array("task"=>"public-forgot-pass2"));
			if(empty($params))$this->_redirect($this->_actionMain);
			$params['newpass'] = $new_pass;
			$params['code']    = $this->_arrParam['code'];
			$tblUser ->saveItem($params, array('task'=>'user-resetpass'));
			$mail = new Zendvn_Mail();
			$success = $mail->mailResetPass($params);
				if($success){
					$this->_helper->flashMessenger->addMessage('Thành công. Chúng tôi sẽ gửi đường dẫn reset khẩu  cho bạn vào hộp thư đến, vui lòng check mail');
					$this->_helper->_redirector('contact');
				}else{
					$this->_helper->flashMessenger->addMessage('Lỗi! Không thể gửi email được, vui lòng liên hệ BQT để được trợ giúp!');
					$this->_helper->_redirector('contact');
				}
		}else{
			$this->_redirect($this->_actionMain);
		}
	}
	public function lostPassAction(){
		$template_path = TEMPLATE_PATH . "/public/system";
		$this->loadTemplate($template_path,'template.ini','template');
		$this->view->Title 		= 'Quên mật khẩu';
		$this->view->headTitle($this->view->Title,true);
		
		$captcha 				= new Zendvn_Captcha_Image();
		//9.Truy gia tri cua CAPTCHA ra VIEW
		$this->view->captcha 	= $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();
		
		if($this->_request->isPost()){
			$captchaID = $this->_arrParam['captchaID'];		
			$validator = new Default_Form_ValidateEmail($this->_arrParam);
			if($validator->isError() == true){				
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();		
			}
			else{
				$arrParam = $validator->getData();
				// Gui email
				$tblUser = new Zendvn_Models_Users();
				$this->_arrParam['random_code'] = md5(time());
				$params = $tblUser ->getItem($this->_arrParam, array("task"=>"public-forgot-pass"));
				
				
				$link = "http://".$_SERVER['HTTP_HOST']. $this->view->url(array(
					'module'	=>'default',
					'controller'=>'public',
					'action'	=>'reset-pass',
					'lang'		=>'vi',
					'code'		=>$this->_arrParam['random_code']
				),'resetpass');
				$params['link'] = $link;
				$mail = new Zendvn_Mail();
				$success = $mail->mailForgotPass($params);
			}
			$captcha->removeImg($captchaID);
		}
		$this->view->messages = $this->_helper->flashMessenger->getMessages();
		$captcha->removeImg($this->view->captcha_id,array('exception'=>true));
	}
	public function userAction(){
		$info = new Zendvn_System_Info();
		$memberInfo = $info->getMemberInfo('id');
		if($memberInfo != ''){
			$tbltUser 				= new Zendvn_Models_Users();
			$this->_arrParam['id']  = $memberInfo; 
			$this->view->Item 		= $tbltUser->getItem($this->_arrParam,array('task'=>'public'));
		}	else
			$this->_redirect('http://'.$_SERVER['HTTP_HOST'].$this->view->url(array(
				'module'=>'default',
				'controller'=>'public',
				'action'=>'login',
				'lang'=>'vi'),'login')
			);
			
		if($this->_request->isPost()){
			$validator = new Default_Form_ValidateUserupdate($this->_arrParam);
			if($validator->isError() 	  == true){				
				$this->view->messageError = $validator->getMessageError();
				$items 		  = $validator->getData();
				
				$this->view->Item = array_merge($this->view->Item, $items);
			}else{
				$arrParam = $validator->getData(array('upload'=>false));
				$tblUser 	= new Zendvn_Models_Users();
				$arrParam['id'] 	= $memberInfo;
				$tblUser->saveItem($arrParam,array('task'=>'user-edit'));
				$this->_redirect('http://'.$_SERVER['HTTP_HOST'].$this->view->url(array(
					'module'=>'default',
					'controller'=>'public',
					'action'=>'user',
					'lang'=>'vi'),'user')
				);
			}
		}
		$this->view->Title 		= 'Thông tin của bạn';
		$this->view->headTitle($this->view->Title,true);
		$template_path = TEMPLATE_PATH . "/public/system";
		$this->loadTemplate($template_path,'template.ini','template');
	}
	public function noticeAction(){
			$template_path = TEMPLATE_PATH . "/public/system";
			$this->loadTemplate($template_path,'template.ini','template');
	}
}




